package cxt.com.recyclerview.demo3;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cxt.com.recyclerview.R;
import cxt.com.recyclerview.demo1.DataModel;

/**
 * 作者：chengxiangtong on 2017/1/10 10:05
 * 邮箱：528440900@qq.com
 */

public class NormalAdapter extends RecyclerView.Adapter<NormalAdapter.VH> {

    public List<DataModel> modelList;
    private OnStartDragListener mOnStartDragListener;

    public NormalAdapter(List<DataModel> modelList, OnStartDragListener mOnStartDragListener) {
        this.modelList = modelList;
        this.mOnStartDragListener = mOnStartDragListener;
    }

    @Override
    public VH onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_3, parent, false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(final VH holder, int position) {
        DataModel dataModel = modelList.get(position);
        holder.textView.setText(dataModel.getTitle());
        holder.imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mOnStartDragListener.startDrag(holder);
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return modelList.size();
    }

    class VH extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;

        public VH(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.iamgeView);
            textView = (TextView) itemView.findViewById(R.id.text3);
        }
    }
}
